<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css">
  <script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhangyh.fun","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Dockerfile 文件详解">
<meta property="og:type" content="article">
<meta property="og:title" content="Dockerfile 文件详解">
<meta property="og:url" content="https://zhangyh.fun/2021/08/23/Dockerfile/index.html">
<meta property="og:site_name" content="XiaoPang の Blog">
<meta property="og:description" content="Dockerfile 文件详解">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-23T07:40:00.000Z">
<meta property="article:modified_time" content="2021-08-23T10:06:19.656Z">
<meta property="article:author" content="XiaoPang">
<meta property="article:tag" content="Dockerfile">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://zhangyh.fun/2021/08/23/Dockerfile/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Dockerfile 文件详解 | XiaoPang の Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?54b35e78bac318b678b24fbb33049203";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


</head>

<body itemscope itemtype="http://schema.org/WebPage">

  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">XiaoPang の Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
    <script src="https://v1.hitokoto.cn/?c=f&encode=js&select=%23hitokoto" defer></script>

      <p class="site-subtitle" itemprop="description" id="hitokoto"><span id="hitokoto_text">记录生活中的点点滴滴</span></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">19</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">11</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">34</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhangyh.fun/2021/08/23/Dockerfile/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="XiaoPang">
      <meta itemprop="description" content="少说，多做">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XiaoPang の Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Dockerfile 文件详解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于：</span>
              

              <time title="创建时间：2021-08-23 15:40:00 / 修改时间：18:06:19" itemprop="dateCreated datePublished" datetime="2021-08-23T15:40:00+08:00">2021-08-23</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于：</span>
                <time title="修改时间：2021-08-23 18:06:19" itemprop="dateModified" datetime="2021-08-23T18:06:19+08:00">2021-08-23</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/docker/" itemprop="url" rel="index"><span itemprop="name">docker</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <br>
            <span class="post-meta-item" title="本文字数">
            <span class="post-meta-item-icon">
            <i class="fa fa-file-word"></i>
            </span>
           <span class="post-meta-item-text">本文字数：</span>
           <span title="post.wordcount">
                2k 字
            </span>
            </span>
            <span class="post-meta-item" title="阅读时长">
            <span class="post-meta-item-icon">
            <i class="fa fa-clock"></i>
            </span>
          <span class="post-meta-item-text">阅读时长：</span>
           <span title="post.min2read">
                7 分钟
            </span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><strong>Dockerfile 文件详解</strong></p>
<span id="more"></span>
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><ul>
<li><code>Dockerfile</code> 是一个用来构建镜像的文本文件，文本内容包含一条条构建镜像所需的指令和说明。</li>
<li><code>#</code> 是 <code>Dockerfile</code> 中的注释。</li>
<li><code>Dockerfile</code> 基本结构：基础镜像、镜像元信息、镜像操作指令、容器启动时执行命令。</li>
<li><code>Docker</code> 自上而下顺序执行 <code>Dockerfile</code> 的指令，文件命名 <code>Dockerfile</code>。<h2 id="Dockerfile-指令"><a href="#Dockerfile-指令" class="headerlink" title="Dockerfile 指令"></a><code>Dockerfile</code> 指令</h2><h3 id="FROM"><a href="#FROM" class="headerlink" title="FROM"></a><code>FROM</code></h3></li>
<li>指定基础镜像，必须是第一条指令。</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">FROM image</span><br><span class="line">FROM image:&lt;tag&gt;</span><br><span class="line"># tag 是可选项，默认值 latest</span><br></pre></td></tr></table></figure>

<h3 id="RUN"><a href="#RUN" class="headerlink" title="RUN"></a><code>RUN</code></h3><ul>
<li>运行指定的命令</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">RUN &lt;command&gt;</span><br><span class="line">RUN [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</span><br><span class="line"># 第一种后面直接跟shell 命令</span><br><span class="line"># Linux 默认：/bin/sh -c</span><br><span class="line"># windows 默认：cmd /S /C</span><br><span class="line"># 第二种类似于函数调用</span><br><span class="line"># executable 相当于 可执行文件，param* 是两个参数</span><br><span class="line"># 举例：</span><br><span class="line">RUN /bin/bash -c source $HOME/.bashrc; echo $HOME</span><br><span class="line">RUN [&quot;/bin/bash&quot;, &quot;-c&quot;, &quot;echo hello&quot;]</span><br><span class="line"># 注意：多行命令不要写多个RUN，原因是Dockerfile 中每一个指令都会建立一层</span><br><span class="line">有可能造成镜像的臃肿、多层，不仅增加构建部署的时间，还容易出错。</span><br></pre></td></tr></table></figure>
<h3 id="CMD"><a href="#CMD" class="headerlink" title="CMD"></a><code>CMD</code></h3><ul>
<li>容器启动运行命令</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">CMD [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</span><br><span class="line">CMD [&quot;param1&quot;, &quot;param2&quot;]</span><br><span class="line">CMD command param1 param2</span><br><span class="line"># 举例：</span><br><span class="line">CMD [&quot;sh&quot;, &quot;-c&quot;, &quot;echo $HOME&quot;]</span><br><span class="line">CMD [&quot;echo&quot;, &quot;$HOME&quot;]</span><br><span class="line"># 注意：参数一定要使用双引号。</span><br><span class="line"># 原因：参数传递后，docker 解析的是一个 JSON array</span><br></pre></td></tr></table></figure>
<ul>
<li><code>RUN</code> 是构建容器运行的命令以及提交运行结果</li>
<li><code>CMD</code> 是容器启动时执行的命令，在构建时并不运行。</li>
</ul>
<h3 id="LABEL"><a href="#LABEL" class="headerlink" title="LABEL"></a><code>LABEL</code></h3><ul>
<li>镜像指定标签</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">LABEL &lt;key&gt;=&lt;value&gt; &lt;key&gt;=&lt;value&gt;</span><br><span class="line"># 注：一个Dockerfile 可以有多个LABEL, 但是不建议写多个，如果太长需要换行可以使用\</span><br><span class="line"># 例：</span><br><span class="line">LABEL key1=value1 \</span><br><span class="line">key2=value2 \</span><br><span class="line">key3=value3</span><br></pre></td></tr></table></figure>
<h3 id="MAINTAINER"><a href="#MAINTAINER" class="headerlink" title="MAINTAINER"></a><code>MAINTAINER</code></h3><ul>
<li>指定作者</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MAINTAINER &lt;name&gt;</span><br></pre></td></tr></table></figure>

<h3 id="EXPOSE"><a href="#EXPOSE" class="headerlink" title="EXPOSE"></a><code>EXPOSE</code></h3><ul>
<li>为暴露容器运行时的监听端口给外部，但是 <code>EXPOSE</code> 并不会使容器访问主机的端口，如果想使容器与主机端口有映射关系，必须在容器启动的时候加上 <code>-P</code> 参数。</li>
</ul>
<h3 id="ENV"><a href="#ENV" class="headerlink" title="ENV"></a><code>ENV</code></h3><ul>
<li>设置环境变量</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ENV &lt;key&gt; &lt;value&gt;</span><br><span class="line">ENV &lt;key&gt;=&lt;value&gt;</span><br><span class="line"># 语法区别：第一种是一次设置一个，第二种可以一次设置多个</span><br></pre></td></tr></table></figure>

<h3 id="ADD"><a href="#ADD" class="headerlink" title="ADD"></a><code>ADD</code></h3><ul>
<li>复制命令，将文件复制到镜像中。</li>
<li>相当于<code>scp</code>。只是<code>scp</code> 需要将用户名和密码的权限验证，而<code>ADD</code> 不用。</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ADD &lt;src&gt; &lt;dest&gt;</span><br><span class="line">ADD [&quot;src&quot;, &quot;dest&quot;]</span><br><span class="line"># &lt;dest&gt; 路径可以是容器内的绝对路径，也可以是工作目录的相对路径</span><br><span class="line"># &lt;src&gt; 可以是本地文件，也可以是一个 url</span><br><span class="line"># 例：</span><br><span class="line">ADD test path_dir/</span><br><span class="line">ADD http://www.baidu.com/ /</span><br><span class="line"># 尽量不要把&lt;src&gt; 写成一个文件夹，如果&lt;src&gt; 是一个文件夹，复制整个目录的内容，包括文件系统元文件。</span><br></pre></td></tr></table></figure>

<h3 id="COPY"><a href="#COPY" class="headerlink" title="COPY"></a><code>COPY</code></h3><ul>
<li>复制命令</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">COPY &lt;src&gt; &lt;dest&gt;</span><br><span class="line">COPY [&quot;&lt;src&gt;&quot;, &quot;&lt;dest&gt;&quot;]</span><br><span class="line"># 与 ADD 区别，COPY 的 &lt;src&gt; 只能是本地文件，其他用法一致</span><br></pre></td></tr></table></figure>
<h3 id="ENTRYPOINT"><a href="#ENTRYPOINT" class="headerlink" title="ENTRYPOINT"></a><code>ENTRYPOINT</code></h3><ul>
<li>功能是启动时的默认命令</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">ENTRYPOINT [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;] </span><br><span class="line">ENTRYPOINT command param1 param2</span><br><span class="line"></span><br><span class="line"># 如果从上到下看到这里的话，那么你应该对这两种语法很熟悉啦。</span><br><span class="line"># 第二种就是写shell</span><br><span class="line"># 第一种就是可执行文件加参数</span><br><span class="line"># 与CMD比较说明（这俩命令太像了，而且还可以配合使用）：</span><br><span class="line"># 1. 相同点：</span><br><span class="line">-   只能写一条，如果写了多条，那么只有最后一条生效</span><br><span class="line">-   容器启动时才运行，运行时机相同</span><br><span class="line"># 2. 不同点：</span><br><span class="line">-   ENTRYPOINT不会被运行的command覆盖，而CMD则会被覆盖</span><br><span class="line">-   如果我们在Dockerfile种同时写了ENTRYPOINT和CMD，并且CMD指令不是一个完整的可执行命令，那么CMD指定的内容将会作为ENTRYPOINT的参数</span><br><span class="line"># 如下：</span><br><span class="line">FROM ubuntu</span><br><span class="line">ENTRYPOINT [&quot;top&quot;, &quot;-b&quot;]  </span><br><span class="line">CMD [&quot;-c&quot;]</span><br><span class="line"></span><br><span class="line"># 如果我们在Dockerfile种同时写了ENTRYPOINT和CMD，并且CMD是一个完整的指令，那么它们两个会互相覆盖，谁在最后谁生效</span><br><span class="line"># 如下：</span><br><span class="line">FROM ubuntu</span><br><span class="line">ENTRYPOINT [&quot;top&quot;, &quot;-b&quot;]  </span><br><span class="line">CMD ls -al</span><br><span class="line"># 那么将执行ls -al ,top -b不会执行。</span><br></pre></td></tr></table></figure>

<h3 id="VOLUME"><a href="#VOLUME" class="headerlink" title="VOLUME"></a><code>VOLUME</code></h3><ul>
<li>可实现挂载功能，可以将内地文件夹或者其他容器种得文件夹挂在到这个容器种</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">VOLUME [&quot;/data&quot;]</span><br><span class="line"># 说明：</span><br><span class="line"># [&quot;/data&quot;]可以是一个JsonArray ，也可以是多个值。所以如下几种写法都是正确的</span><br><span class="line">VOLUME [&quot;/var/log/&quot;]</span><br><span class="line">VOLUME /var/log</span><br><span class="line">VOLUME /var/log /var/db</span><br><span class="line"># 一般的使用场景为需要持久化存储数据时</span><br><span class="line"># 容器使用的是AUFS，这种文件系统不能持久化数据，当容器关闭后，所有的更改都会丢失。</span><br><span class="line"># 所以当数据需要持久化时用这个命令。</span><br></pre></td></tr></table></figure>
<h3 id="USER"><a href="#USER" class="headerlink" title="USER"></a><code>USER</code></h3><ul>
<li>设置启动容器的用户，可以是用户名或UID，所以，只有下面的两种写法是正确的</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">USER daemo</span><br><span class="line">USER UID</span><br><span class="line"># 注意：如果设置了容器以daemon用户去运行，那么RUN, CMD 和 ENTRYPOINT 都会以这个用户去运行</span><br></pre></td></tr></table></figure>
<h3 id="WORKDIR"><a href="#WORKDIR" class="headerlink" title="WORKDIR"></a><code>WORKDIR</code></h3><p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">WORKDIR /path/to/workdir</span><br><span class="line"># 设置工作目录，对RUN,CMD,ENTRYPOINT,COPY,ADD生效。如果不存在则会创建，也可以设置多次。</span><br><span class="line"># 如：</span><br><span class="line">WORKDIR /a</span><br><span class="line">WORKDIR b</span><br><span class="line">WORKDIR c</span><br><span class="line">RUN pwd</span><br><span class="line"># pwd执行的结果是/a/b/c</span><br><span class="line"># WORKDIR也可以解析环境变量</span><br><span class="line"># 如：</span><br><span class="line">ENV DIRPATH /path</span><br><span class="line">WORKDIR $DIRPATH/$DIRNAME</span><br><span class="line">RUN pwd</span><br><span class="line"># pwd的执行结果是/path/$DIRNAME</span><br></pre></td></tr></table></figure>
<h3 id="ARG"><a href="#ARG" class="headerlink" title="ARG"></a><code>ARG</code></h3><p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">ARG &lt;name&gt;[=&lt;default value&gt;]</span><br><span class="line"># 设置变量命令，ARG命令定义了一个变量，在docker build创建镜像的时候，使用 --build-arg &lt;varname&gt;=&lt;value&gt;来指定参数</span><br><span class="line"># 如果用户在build镜像时指定了一个参数没有定义在Dockerfile种，那么将有一个Warning</span><br><span class="line"># 提示如下：</span><br><span class="line">[Warning] One or more build-args [foo] were not consumed.**</span><br><span class="line"># 我们可以定义一个或多个参数，如下：</span><br><span class="line">FROM busybox</span><br><span class="line">ARG user1</span><br><span class="line">ARG buildno</span><br><span class="line"># 也可以给参数一个默认值：</span><br><span class="line">FROM busybox</span><br><span class="line">ARG user1=someuser</span><br><span class="line">ARG buildno=1</span><br><span class="line"># 如果我们给了ARG定义的参数默认值，那么当build镜像时没有指定参数值，将会使用这个默认值</span><br></pre></td></tr></table></figure>
<h3 id="ONBUILD"><a href="#ONBUILD" class="headerlink" title="ONBUILD"></a><code>ONBUILD</code></h3><p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ONBUILD [INSTRUCTION]</span><br><span class="line"># 这个命令只对当前镜像的子镜像生效。</span><br><span class="line"># 比如当前镜像为A，在Dockerfile种添加：</span><br><span class="line">ONBUILD RUN ls -al</span><br><span class="line"># 这个 ls -al 命令不会在A镜像构建或启动的时候执行</span><br><span class="line"># 此时有一个镜像B是基于A镜像构建的，那么这个ls -al 命令会在B镜像构建的时候被执行。</span><br></pre></td></tr></table></figure>
<h3 id="STOPSIGNAL"><a href="#STOPSIGNAL" class="headerlink" title="STOPSIGNAL"></a><code>STOPSIGNAL</code></h3><p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">STOPSIGNAL signal</span><br><span class="line">STOPSIGNAL命令是的作用是当容器推出时给系统发送什么样的指令</span><br></pre></td></tr></table></figure>

<h3 id="HEALTHCHECK"><a href="#HEALTHCHECK" class="headerlink" title="HEALTHCHECK"></a><code>HEALTHCHECK</code></h3><ul>
<li>容器健康状况检查命令</li>
</ul>
<p>语法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">HEALTHCHECK [OPTIONS] CMD command</span><br><span class="line">HEALTHCHECK NONE</span><br><span class="line"># 第一个的功能是在容器内部运行一个命令来检查容器的健康状况</span><br><span class="line"># 第二个的功能是在基础镜像中取消健康检查命令</span><br><span class="line"># [OPTIONS]的选项支持以下三中选项：</span><br><span class="line">--interval=DURATION 两次检查默认的时间间隔为30秒</span><br><span class="line">--timeout=DURATION 健康检查命令运行超时时长，默认30秒</span><br><span class="line">--retries=N 当连续失败指定次数后，则容器被认为是不健康的，状态为unhealthy，默认次数是3</span><br><span class="line"># 注意：</span><br><span class="line"># HEALTHCHECK命令只能出现一次，如果出现了多次，只有最后一个生效。</span><br><span class="line"># CMD后边的命令的返回值决定了本次健康检查是否成功，具体的返回值如下：</span><br><span class="line">	0: success - 表示容器是健康的</span><br><span class="line">	1: unhealthy - 表示容器已经不能工作了</span><br><span class="line">	2: reserved - 保留值</span><br><span class="line"># 例子：</span><br><span class="line">HEALTHCHECK --interval=5m --timeout=3s \</span><br><span class="line">CMD curl -f http://localhost/ || exit 1</span><br><span class="line"># 健康检查命令是：</span><br><span class="line">curl -f http://localhost/ || exit 1</span><br><span class="line"># 两次检查的间隔时间是5秒</span><br><span class="line"># 命令超时时间为3秒</span><br></pre></td></tr></table></figure>

<h2 id="Dockerfile-构建镜像"><a href="#Dockerfile-构建镜像" class="headerlink" title="Dockerfile 构建镜像"></a><code>Dockerfile</code> 构建镜像</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t 镜像名:标签名 Dockerfile路径（可以是绝对路径，也可以是相对路径）</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>XiaoPang
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zhangyh.fun/2021/08/23/Dockerfile/" title="Dockerfile 文件详解">https://zhangyh.fun/2021/08/23/Dockerfile/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://zhangyh.fun/2021/08/23/Dockerfile/"  aria-label="复制成功！"></i></span>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({
          title: "",
          text: '复制成功',
          icon: "success",
          showConfirmButton: true
          });
        });
    });
</script>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Dockerfile/" rel="tag"><i class="fa fa-tag"></i> Dockerfile</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/23/1646.%20%E8%8E%B7%E5%8F%96%E7%94%9F%E6%88%90%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" rel="prev" title="1646. 获取生成数组中的最大值">
      <i class="fa fa-chevron-left"></i> 1646. 获取生成数组中的最大值
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/24/82.%20%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0%20II/" rel="next" title="82. 删除排序链表中的重复元素 II">
      82. 删除排序链表中的重复元素 II <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    

  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.</span> <span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Dockerfile-%E6%8C%87%E4%BB%A4"><span class="nav-number">2.</span> <span class="nav-text">Dockerfile 指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#FROM"><span class="nav-number">2.1.</span> <span class="nav-text">FROM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#RUN"><span class="nav-number">2.2.</span> <span class="nav-text">RUN</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CMD"><span class="nav-number">2.3.</span> <span class="nav-text">CMD</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#LABEL"><span class="nav-number">2.4.</span> <span class="nav-text">LABEL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MAINTAINER"><span class="nav-number">2.5.</span> <span class="nav-text">MAINTAINER</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#EXPOSE"><span class="nav-number">2.6.</span> <span class="nav-text">EXPOSE</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ENV"><span class="nav-number">2.7.</span> <span class="nav-text">ENV</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ADD"><span class="nav-number">2.8.</span> <span class="nav-text">ADD</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#COPY"><span class="nav-number">2.9.</span> <span class="nav-text">COPY</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ENTRYPOINT"><span class="nav-number">2.10.</span> <span class="nav-text">ENTRYPOINT</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#VOLUME"><span class="nav-number">2.11.</span> <span class="nav-text">VOLUME</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#USER"><span class="nav-number">2.12.</span> <span class="nav-text">USER</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#WORKDIR"><span class="nav-number">2.13.</span> <span class="nav-text">WORKDIR</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ARG"><span class="nav-number">2.14.</span> <span class="nav-text">ARG</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ONBUILD"><span class="nav-number">2.15.</span> <span class="nav-text">ONBUILD</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#STOPSIGNAL"><span class="nav-number">2.16.</span> <span class="nav-text">STOPSIGNAL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HEALTHCHECK"><span class="nav-number">2.17.</span> <span class="nav-text">HEALTHCHECK</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Dockerfile-%E6%9E%84%E5%BB%BA%E9%95%9C%E5%83%8F"><span class="nav-number">3.</span> <span class="nav-text">Dockerfile 构建镜像</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="XiaoPang"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">XiaoPang</p>
  <div class="site-description" itemprop="description">少说，多做</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhang123-123" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhang123-123" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/1741290057@qq.com" title="E-Mail → 1741290057@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


<!-- CloudCalendar -->
<div class="widget-wrap" style="width: 90%;margin-left: auto;margin-right: auto; opacity: 0.97;">
	<div class="widget" id="CloudCalendar"></div>
</div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoPang</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
    <script type="text/javascript" color="0,0,0" opacity='0.5' zIndex="-2" count="200" src="/js/my_js/canvas-nest.min.js"></script>
    

</body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/my_js/clicklove.js"></script>

<!-- calendar widget -->

    <script src="/lib/calendar/calendar.min.js"></script>
    <script src="/lib/calendar/languages.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $('#CloudCalendar').aCalendar('zh-CN',
            $.extend(
                '', {
                    single:true,
                    root:'/calendar/'
                }
            )
        );
    });
    </script>


</html>
